package developer.zyk.ability.knowledge.parsers.fileParser;

import developer.zyk.global.common.consts.FileType;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhangyongkang
 * @time 2025/5/15 10:19
 * @description
 */
@Component
public class FileParserHandlerFactory {

    private Map<FileType, FileParserHandler> fileParserHandlerMap;

    @Resource
    public void setFileParserHandlerMap(
            List<FileParserHandler> handlers
    ) {
        fileParserHandlerMap = new HashMap<>();
        handlers.forEach(a -> {
            for (FileType supportFileType : a.supportFileType()) {
                fileParserHandlerMap.put(supportFileType, a);
            }
        });
    }

    public FileParserHandler getHandler(FileType fileType) {
        FileParserHandler fileParserHandler = fileParserHandlerMap.get(fileType);
        if (Objects.isNull(fileParserHandler)) {
            throw new UnsupportedOperationException("不支持的文件类型" + fileType);
        }
        return fileParserHandler;
    }
}
